import axios from 'axios';

const API_BASE_URL = import.meta.env.VITE_BASE_URL; 
// 使用 axios.create() 创建了一个自定义配置的 axios 实例 api，设置了基础 URL 和默认请求头JSON 格式
const api = axios.create({
    baseURL: API_BASE_URL,
    headers: {
        'Content-Type': 'application/json',//表示客户端发送的请求体（body）数据格式是 JSON 格式
    },
});

// 登录
export const login = (username: string, password: string) => {
    return api.post('/users/login', { username, password });
};

// 注册
export const register = (username: string, email: string, password: string) => {
    return api.post('/users/register', { username, email, password, role: 'admin' });
};

// 修改密码
export const updatePassword = (currentPassword: string, newPassword: string, confirmPassword: string, token: string) => {
    return api.post(
        '/users/update-password',
        { currentPassword, newPassword, confirmPassword },
        { headers: { Authorization: `Bearer ${token}` } }
    );
};

// 忘记密码（获取重置链接）
export const forgotPassword = (email: string) => {
    return api.post('/users/forgot-password', { email });
};

// 重置密码
export const resetPassword = (token: string, newPassword: string, confirmPassword: string) => {
    return api.post('/users/reset-password', { token, newPassword, confirmPassword });
};

// 获取当前用户信息
export const getProfile = (token: string) => {
    return api.get('/users/profile', {
        headers: { Authorization: `Bearer ${token}` }
    });
};

export default api;
